//
//  ExamineTwoViewController.m
//  LoanOne
//
//  Created by WangZhenyu on 2017/10/26.
//  Copyright © 2017年 WangZhenyu. All rights reserved.
//

#import "ExamineTwoViewController.h"
#import "ExamineTabBar.h"
#import "MJRefresh.h"
#import "NoContentView.h"
#import "ExamineTwoTableViewCell.h"
#import "GlobalFunction.h"
#import "WebShowViewController.h"

@interface ExamineTwoViewController (){
    ExamineTabBar                    *tabBarView;
    NSMutableArray                  *contentMutArr;
    NSInteger                       pageNum;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ExamineTwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"资讯";
    tabBarView = [ExamineTabBar sharedExamineTabBar];
    [self.navigationItem setHidesBackButton:YES];
    
    pageNum = 0;
    contentMutArr = [NSMutableArray array];
    
    //集成上下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshHeader)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFooter)];
    
    [self requestContent:YES];
    
    [self.tableView setFrame:CGRectMake(0, 64 + IS_iPhoneX_Top, SCREEN_WIDTH_DEVICE, SCREEN_HEIGHT_DEVICE - 64 - IS_iPhoneX_Top - IS_iPhoneX_Bottom - 50)];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.fd_interactivePopDisabled = YES;
    [self.view addSubview:tabBarView];
    [tabBarView setCurrentViewControllerIndex:2];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.fd_interactivePopDisabled = NO;
}

- (void)refreshHeader{
    pageNum = 0;
    [self requestContent:NO];
}

- (void)refreshFooter{
    pageNum = ++pageNum;
    [self requestContent:NO];
}

//请求数据
- (void)requestContent:(BOOL)alert{
    if (alert) {
        [self loadingHUDAlert:@"正在加载"];
    }
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:[NSNumber numberWithInteger:5] forKey:@"catid"];
    [dic setObject:[NSNumber numberWithInteger:pageNum] forKey:@"page"];
    [dic setObject:[NSNumber numberWithInteger:2] forKey:@"platform"];
    
    [[ServiceRequest sharedService] POSTCUSTOM:@"http://api.jpm.cn/letter/information" parameters:dic success:^(id responseObject) {
        [self hideHudAlert];
        if (pageNum == 0) {
            [self.tableView.mj_header endRefreshing];
            [contentMutArr removeAllObjects];
            [self.tableView.mj_footer resetNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        NSArray *arr = [responseObject objectForKey:@"data"];
        for (int i = 0; i < arr.count; i++) {
            [contentMutArr addObject:[arr objectAtIndex:i]];
        }
        if (contentMutArr.count == 0) {
            self.tableView.tableFooterView = [[NoContentView alloc] initItemFrame:self.tableView.frame imageType:InfoImageType_Default title:@"暂无资讯内容"];
        }else{
            self.tableView.tableFooterView = nil;
        }
        if (pageNum > 0 && arr.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableView reloadData];
        
    } failure:^(NSString *error, NSInteger code) {
        [self hideHudAlert];
        if (pageNum == 0) {
            [self.tableView.mj_header endRefreshing];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        if (code == 0) {
            self.tableView.tableFooterView = [[NoContentView alloc] initItemFrame:self.tableView.frame imageType:InfoImageType_NoNetWork title:error];
        }else if (code == 404){
            if (pageNum == 0) {
                [contentMutArr removeAllObjects];
                self.tableView.tableFooterView = [[NoContentView alloc] initItemFrame:self.tableView.frame imageType:InfoImageType_Default title:@"暂无资讯内容"];
                [self.tableView reloadData];
            }
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self showHUDAlert:error];
        }
    }];

    [self hideHudAlert];
}

#pragma mark - UITableView datasource methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [contentMutArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"ExamineTwoCell";
    ExamineTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ExamineTwoTableViewCell" owner:self options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSDictionary *dic = [contentMutArr objectAtIndex:indexPath.row];
    [cell.contentImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"imgurl"]]];
    NSString *showTime = compareDate([dic[@"show_time"] floatValue]);
    cell.oneLabel.text = [NSString stringWithFormat:@"%@   %@", showTime, [dic objectForKey:@"nickname"]];
    cell.twoLabel.text = [dic objectForKey:@"title"];
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
    NSDictionary *dic = [contentMutArr objectAtIndex:indexPath.row];
    WebShowViewController *viewCon = [[WebShowViewController alloc] initWithNibName:@"WebShowViewController" bundle:nil];
    viewCon.urlStr = [dic objectForKey:@"url"];
    [self.navigationController pushViewController:viewCon animated:YES];
}
@end
